package com.casper.codechef;

import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class Main {

	public static final int MAX_TEST_COUNT = 10000;
	public static final int MAX_LENGTH = 20000;
	public static final int MAX_OVERALL_LENGTH = 200000;

	private static int[] getLucky(int[] array) {
		return getMax(array);
	}

	private static int[] getMax(int[] numbers) {
		int[] temp = new int[numbers.length / 2];
		int j = 0;
		for (int i = 0; i < numbers.length; i += 2) {
			temp[j] = (getMaxNumber(numbers[i], numbers[i + 1]));
			j++;
		}
		return temp;

	}

	private static int getMaxNumber(int number1, int number2) {
		int[] array1 = shuffle(getIntAsIntArray(number1));
		int[] array2 = shuffle(getIntAsIntArray(number2));

		StringBuilder output = new StringBuilder(array1.length);

		for (int i = 0; i < array1.length; i++) {
			int temp = Math.max(array1[i], array2[i]);
			if (temp == 7 || temp == 4)
				output.append(temp);
		}
		if (output.length() != 0)
			return Integer.parseInt(output.toString().trim());
		else
			return 0;

	}

	private static int[] getIntAsIntArray(int value) {
		String temp = Integer.toString(value);
		int[] newInt = new int[temp.length()];
		for (int i = 0; i < temp.length(); i++) {
			newInt[i] = temp.charAt(i) - '0';
		}
		return newInt;
	}

	private static int[] shuffle(int[] value) {

		for (int i = value.length; i >= 1; i--)
			value = swap(value, i - 1, new Random().nextInt(i));
		return value;

	}

	private static int[] swap(int[] arr, int i, int j) {
		int tmp = arr[i];
		arr[i] = arr[j];
		arr[j] = tmp;
		return arr;
	}

	Main() throws IOException {
		int[] array;
		Scanner bufferedReader = new Scanner(System.in);
		int mTestCases = bufferedReader.nextInt();
		if (mTestCases > MAX_TEST_COUNT)
			throw new IOException("Maximum text count is " + MAX_TEST_COUNT);
		array = new int[mTestCases * 2];
		for (int i = 0; i < mTestCases * 2; i++) {
			array[i] = bufferedReader.nextInt();
		}
		int[] resultArray = getLucky(array);
		for (int i = 0; i < resultArray.length; i++)
			if (resultArray[i] != 0)
				System.out.println(resultArray[i]);
			else
				System.out.println("");

	}

	public static void main(String[] args) throws IOException {
		new Main();
	}
}
